package com.view;

/**
 * 登录界面
 */

import com.DAL.DatabaseConnection;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Date;

public class LoginView extends JFrame {
    JLabel titleLabel = new JLabel("CC小区物业管理系统", JLabel.CENTER);
    SpringLayout springLayout = new SpringLayout();
    JPanel centerPanel = new JPanel(springLayout);
    JLabel userNameLabel = new JLabel("账号：");
    JTextField userTxt = new JTextField();
    JLabel pwdLabel = new JLabel("密码：");
    JPasswordField pwdTxt = new JPasswordField();
    JLabel verificationLabel = new JLabel("验证码：");
    JTextField verificationTxt = new JTextField();//实例化验证码框

    //TODO:相对路径哪里错了
    //绝对路径：C:\Users\caiyo\文件共享\工程文件\IDEA\PropertyManagement\src\resource\Verify7364 set.jpeg
    //相对路径：resource/Verify7364.jpeg
    JLabel verifyPic = new JLabel(new ImageIcon("C:\\Users\\caiyo\\文件共享\\工程文件\\IDEA\\PropertyManagement\\src\\resource\\Verify7364 set.jpeg"));

    //TODO：提取按钮触发事件的功能
    JButton Logup = new JButton(new AbstractAction("注册") {
        @Override
        public void actionPerformed(ActionEvent e) {
            new LogupView();
            dispose();
        }
    });
    JButton Login = new JButton(new AbstractAction("登录") {
        @Override
        public void actionPerformed(ActionEvent actionEvent) {
            //获取用户名和密码
            String userName = userTxt.getText();
            String pwd = new String(pwdTxt.getPassword());
            String verifyNum = verificationTxt.getText();
            String rightNum = "7364";

            //用户名或者密码及验证码特殊情况判断
            if (userName.equals("") || pwd.equals("")) {
                //TODO：如何设置弹出对话框居中
                JOptionPane.showMessageDialog(titleLabel, "用户名或密码不能为空\n同时请检查你的身份类型", "警告", JOptionPane.WARNING_MESSAGE);
                return;
            } else if (verifyNum.equals("")) {
                JOptionPane.showMessageDialog(titleLabel, "验证码不能为空", "警告", JOptionPane.WARNING_MESSAGE);
                return;
            } else if (!verifyNum.equals(rightNum)) {
                JOptionPane.showMessageDialog(titleLabel, "验证码有误，请重新输入", "警告", JOptionPane.WARNING_MESSAGE);
                return;
            }

            //获取当前时间 TODO:封装获取当前系统时间
            SimpleDateFormat sdf = new SimpleDateFormat();  //格式化时间
            sdf.applyPattern("yyyy-MM-dd HH:mm:ss a");  //a为am/pm的标记
            Date date = new Date(); //获取系统时间

            //进行登录 与数据库进行连接
            System.out.println("数据库脚本日志：");
            Connection conn = DatabaseConnection.getConnection();
            try {
                System.out.println("数据库建立连接成功！" + sdf.format(date));
            } catch (Exception e) {
                e.printStackTrace();
            }

            //查询操作
            boolean check = false;
            try {
                if (adminChoose.isSelected()) {
                    Statement statement = conn.createStatement();
                    ResultSet rs = statement.executeQuery("select * from admin where username ='" + userName + "'");
                    while (rs.next()) {
                        System.out.println("已进入while循环！" + sdf.format(date));
                        if (pwd.equals(rs.getString("password"))) {
                            System.out.println("校对成功！" + sdf.format(date));
                            check = true;
                            JOptionPane.showMessageDialog(titleLabel, "登陆成功！\n你好！管理员" + userName + "！", "提醒", JOptionPane.WARNING_MESSAGE);
                            dispose();
                            new MainView();
                        }
                    }
                    if (!check) {
                        JOptionPane.showMessageDialog(titleLabel, "账号或密码不正确，请重新输入！\n同时请检查你选择的身份类型是否正确！", "提醒", JOptionPane.WARNING_MESSAGE);
                        return;
                    }
                } else {
                    //登陆类型为业主时的代码
                    Statement statement = conn.createStatement();
                    ResultSet rs = statement.executeQuery("select * from owner where name ='" + userName + "'");
                    while (rs.next()) {
                        System.out.println("已进入while循环！" + sdf.format(date));
                        if (pwd.equals(rs.getString("password"))) {
                            System.out.println("校对成功！" + sdf.format(date));
                            check = true;
                            JOptionPane.showMessageDialog(titleLabel, "登陆成功！\n你好！用户" + userName + "！", "提醒", JOptionPane.WARNING_MESSAGE);
                            dispose();
                            new MainView();
                        }
                    }
                    if (!check) {
                        JOptionPane.showMessageDialog(titleLabel, "账号或密码不正确，请重新输入！\n同时请检查你选择的身份类型是否正确！", "提醒", JOptionPane.WARNING_MESSAGE);
                        return;
                    }
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }

        }
    });
    JButton Exit = new JButton(new AbstractAction("退出") {
        @Override
        public void actionPerformed(ActionEvent e) {
            dispose();
        }
    });

    JLabel Choose = new JLabel("用户类型：");
    JRadioButton adminChoose = new JRadioButton("物业管理员");
    JRadioButton ownerChoose = new JRadioButton("小区业主", true);

    //登录界面设计 Method
    public LoginView() {
        super("CC小区物业管理系统 V1.0 beta1");

        Container contentPane = getContentPane();

        //设置字体
        setFont();

        //把组件加入面板
        contentPane.add(titleLabel, BorderLayout.NORTH);

        contentPane.add(centerPanel, BorderLayout.CENTER);
        centerPanel.add(userNameLabel);
        centerPanel.add(userTxt);
        centerPanel.add(pwdLabel);
        centerPanel.add(pwdTxt);
        centerPanel.add(verificationLabel);
        centerPanel.add(verificationTxt);
        centerPanel.add(verifyPic);
        centerPanel.add(Logup);
        centerPanel.add(Login);
        centerPanel.add(Exit);

        ButtonGroup group = new ButtonGroup();
        centerPanel.add(Choose);
        group.add(this.adminChoose);
        group.add(this.ownerChoose);
        centerPanel.add(this.adminChoose);
        centerPanel.add(this.ownerChoose);

        //设置弹簧布局
        springLayout(contentPane);

        //设置窗口图标
        setIcon();


        setSize(600, 800);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);
    }

    //字体设置 Method
    private void setFont() {
        titleLabel.setFont(new Font("华文行楷", Font.PLAIN, 40));
        titleLabel.setPreferredSize(new Dimension(0, 260));

        Font centerFont = new Font("楷体", Font.PLAIN, 20);
        userNameLabel.setFont(centerFont);
        userTxt.setPreferredSize(new Dimension(200, 30));
        pwdLabel.setFont(centerFont);
        pwdTxt.setPreferredSize(new Dimension(200, 30));
        verificationLabel.setFont(centerFont);
        verificationTxt.setPreferredSize(new Dimension(100, 30));
        verifyPic.setPreferredSize(new Dimension(100, 30));
        Logup.setFont(centerFont);
        Login.setFont(centerFont);
        Exit.setFont(centerFont);
        Choose.setFont(centerFont);
        adminChoose.setFont(centerFont);
        ownerChoose.setFont(centerFont);
    }

    //弹簧布局 Method
    private void springLayout(Container contentPane) {

//        Spring titleLabelWidth = Spring.width(userNameLabel);
//        Spring titleTxtWidth = Spring.width(titleTxt);
//        Spring spaceWidth = Spring.constant(20);
//        Spring childWidth = Spring.sum(Spring.sum(titleLabelWidth,titleTxtWidth),spaceWidth);
//        int offsetX = childWidth.getValue() / 2;

        //布局userNameLabel
        Spring childWidth = Spring.sum(Spring.sum(Spring.width(userNameLabel), Spring.width(userTxt)),
                Spring.constant(20));
        int offsetX = childWidth.getValue() / 2;

        springLayout.putConstraint(SpringLayout.WEST, userNameLabel, -offsetX,
                SpringLayout.HORIZONTAL_CENTER, centerPanel);
        springLayout.putConstraint(SpringLayout.NORTH, userNameLabel, 20, SpringLayout.NORTH, centerPanel);
        //布局userTxt
        springLayout.putConstraint(SpringLayout.WEST, userTxt, 20, SpringLayout.EAST, userNameLabel);
        springLayout.putConstraint(SpringLayout.NORTH, userTxt, 0, SpringLayout.NORTH, userNameLabel);
        //布局pwdLabel
        springLayout.putConstraint(SpringLayout.EAST, pwdLabel, 0, SpringLayout.EAST, userNameLabel);
        springLayout.putConstraint(SpringLayout.NORTH, pwdLabel, 40, SpringLayout.SOUTH, userNameLabel);
        //布局pwdTxt
        springLayout.putConstraint(SpringLayout.WEST, pwdTxt, 20, SpringLayout.EAST, pwdLabel);
        springLayout.putConstraint(SpringLayout.NORTH, pwdTxt, 0, SpringLayout.NORTH, pwdLabel);
        //布局verificationLabel
        springLayout.putConstraint(SpringLayout.EAST, verificationLabel, 0, SpringLayout.EAST, pwdLabel);
        springLayout.putConstraint(SpringLayout.NORTH, verificationLabel, 40, SpringLayout.SOUTH, pwdLabel);
        //布局verificationTxt
        springLayout.putConstraint(SpringLayout.WEST, verificationTxt, 20, SpringLayout.EAST, verificationLabel);
        springLayout.putConstraint(SpringLayout.NORTH, verificationTxt, 0, SpringLayout.NORTH, verificationLabel);
        //布局verifyPic
        springLayout.putConstraint(SpringLayout.WEST, verifyPic, 20, SpringLayout.EAST, verificationTxt);
        springLayout.putConstraint(SpringLayout.NORTH, verifyPic, 0, SpringLayout.NORTH, verificationTxt);
        //布局Logup
        springLayout.putConstraint(SpringLayout.EAST, Logup, -40, SpringLayout.EAST, verificationLabel);
        springLayout.putConstraint(SpringLayout.NORTH, Logup, 80, SpringLayout.SOUTH, verificationLabel);
        //布局Login
        springLayout.putConstraint(SpringLayout.EAST, Login, 160, SpringLayout.EAST, Logup);
        springLayout.putConstraint(SpringLayout.NORTH, Login, 0, SpringLayout.NORTH, Logup);
        //布局Exit
        springLayout.putConstraint(SpringLayout.EAST, Exit, 160, SpringLayout.EAST, Login);
        springLayout.putConstraint(SpringLayout.NORTH, Exit, 0, SpringLayout.NORTH, Logup);
        //布局Choose
        springLayout.putConstraint(SpringLayout.EAST, Choose, 40, SpringLayout.WEST, userNameLabel);
        springLayout.putConstraint(SpringLayout.NORTH, Choose, 100, SpringLayout.SOUTH, Logup);
        //布局adminChoose
        springLayout.putConstraint(SpringLayout.WEST, adminChoose, 40, SpringLayout.EAST, Choose);
        springLayout.putConstraint(SpringLayout.NORTH, adminChoose, -5, SpringLayout.NORTH, Choose);
        //布局ownerChoose
        springLayout.putConstraint(SpringLayout.WEST, ownerChoose, 40, SpringLayout.EAST, adminChoose);
        springLayout.putConstraint(SpringLayout.NORTH, ownerChoose, -5, SpringLayout.NORTH, Choose);

        SpringLayout.Constraints titleLabelC = springLayout.getConstraints(titleLabel);
//        titleLabelC.setX(Spring.constant(100));
        titleLabelC.setY(Spring.constant(50));
    }

    //设置窗体图标 Method TODO：抽取此方法提取为Class类
    private void setIcon() {
//        URL resource = FirstScene.class.getClassLoader().getResource("Icon.jpg");
//        Image image = new ImageIcon(resource).getImage();
//        setIconImage(image);
        Image icon = Toolkit.getDefaultToolkit().getImage("src/resource/Icon.jpg");
        this.setIconImage(icon);
    }

    //Main方法
    public static void main(String[] args) {
        new LoginView();
    }

}
